<?php

/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController {

    /**
     * @var string the default layout for the controller view. Defaults to '//layouts/column1',
     * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
     */
    public $layout = '//layouts/column1';

    /**
     * @var array context menu items. This property will be assigned to {@link CMenu::items}.
     */
    public $menu = array();

    /**
     * @var array the breadcrumbs of the current page. The value of this property will
     * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
     * for more details on how to specify this property.
     */
    public $breadcrumbs = array();

    public function filters() {
        return array(
            'accessControl - login - logout', // perform access control for CRUD operations
            'postOnly + delete', // we only allow deletion via POST request
        );
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules() {
        return array(
            array('allow', 'users' => array('@')),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }

    public function getUser($id, $atribute) {
        return Users::model()->findByPk($id)->$atribute;
    }

    public function updateUser($id, $atribute, $value) {
        $user = Users::model()->findByPk($id);
        $user->$atribute = $value;
        if ($user->save(false))
            return true;
        return false;
    }

    public function unreadMessages() {
        $counter = 0;
        $models = News::model()->findAll();
        foreach ($models as $model) {
            $ids = explode(',', $model['news_views']);
            if (!empty($ids))
                if (array_search(Yii::app()->user->id, $ids) === false)
                    $counter++;
        }
        return $counter;
    }

    public function seconds2times($seconds) {
        $times = array();

        $count_zero = false;

        $periods = array(60, 3600, 86400, 31536000);

        for ($i = 3; $i >= 0; $i--) {
            $period = floor($seconds / $periods[$i]);
            if (($period > 0) || ($period == 0 && $count_zero)) {
                $times[$i + 1] = $period;
                $seconds -= $period * $periods[$i];

                $count_zero = true;
            }
        }

        $times[0] = $seconds;
        return $times;
    }

    public function calculateSumm($range, $cityPrice) {
        $c = $range / 3600;
        return ($c * $cityPrice);
    }

    public function recharge($userID, $summ, $time = 0) {
        if ($time == 0)
            $time = time();
        $user = Users::model()->findByPk($userID);
        $user->user_balance = $user->user_balance + $summ;
        $cost = new UserCosts();
        $cost->amount = $summ;
        $cost->date = $time;
        $cost->user_id = $userID;        
        if ($user->save(false) && $cost->save(false))
            return true;
        return false;
    }

}
